package com.notary.tx_live_plugin.util;

import com.notary.tx_live_plugin.widget.TxLivePlayView;

import java.util.List;
import java.util.Map;

/**
 * 直播视频在线 播放
 */
public class PlayManager {

    public static PlayManager getInstance(){
        return PlayManagerHolder.instance;
    }

    private static final class PlayManagerHolder {
        private static final PlayManager instance = new PlayManager();
    }

    private PlayManager(){}

    private TxLivePlayView txLivePlayView;
    private TxLivePlayView.OnPlayCallback onPlayCallback;

    public void setTxLivePlayView(TxLivePlayView txLivePlayView) {
        this.txLivePlayView = txLivePlayView;
        if(txLivePlayView != null){
            txLivePlayView.setOnPlayCallback(this::callback);
        }
    }

    public void setOnPlayCallback(TxLivePlayView.OnPlayCallback onPlayCallback) {
        this.onPlayCallback = onPlayCallback;
    }

    public void clear(){
        setTxLivePlayView(null);
        setOnPlayCallback(null);
    }

    public boolean isPlaying(){
        if(checkPlayView(TxConstant.PlayMethod.isPlaying))return false;
        return txLivePlayView.isPlaying();
    }

    public void startPlay(Object data){
        if(checkPlayView(TxConstant.PlayMethod.startPlay))return;
        List<String> url = null;
        String cacheFile = null;
        if (data instanceof Map) {
            Map<String, Object> d = (Map<String, Object>) data;
            url = (List<String>) d.get(TxConstant.PlayMethod.Key.urls);
            cacheFile = (String) d.get(TxConstant.PlayMethod.Key.cacheFilePath);
        }
        if (url == null || url.size() == 0) {
            callback(TxConstant.PlayMethod.Back.error, "method startPlay need param,urls is empty" + url + ",data " + data.toString());
            return;
        }
        txLivePlayView.startPlay(url,cacheFile);
    }

    public void stopPlay(){
        if(checkPlayView(TxConstant.PlayMethod.stopPlay))return;
        txLivePlayView.stopPlay();
    }

    public void firstPlayRtmp(){
        if(checkPlayView(TxConstant.PlayMethod.firstPlayRtmp))return;
        txLivePlayView.firstPlayRtmp();
    }

    public void onPause(){
        if(checkPlayView(TxConstant.PlayMethod.onPause))return;
        txLivePlayView.onPause();
    }

    public void onResume(){
        if(checkPlayView(TxConstant.PlayMethod.onResume))return;
        txLivePlayView.onResume();
    }

    public void onDestroy(){
        if(checkPlayView(TxConstant.PlayMethod.onDestroy))return;
        txLivePlayView.onDestroy();
    }

    //修改播放进度
    public void seek(Object arguments) {
        if(checkPlayView(TxConstant.PlayMethod.seek))return;
        if (arguments instanceof Map) {
            Map<String, Object> d = (Map<String, Object>) arguments;
            Integer seekProgress = (Integer) d.get(TxConstant.PlayMethod.Key.seekProgress);
            if(seekProgress != null){
                txLivePlayView.seek(seekProgress);
            }
        }
    }

    /**
     * 确认推流控件是否为空
     */
    private boolean checkPlayView(String methodName) {
        if (txLivePlayView == null) {
            callback(TxConstant.PlayMethod.Back.error, "push view is empty for method " + methodName);
            return true;
        }
        return false;
    }

    private void callback(String method, Object data) {
        if (onPlayCallback != null) {
            onPlayCallback.callback(method, data);
        }
    }
}
